home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 April / PCWorld_2008-04_cd.bin / komercni software / miton / SystemMechanic7Pro.exe / {app} / smhtml.dll / 1033 / HTML / CHECKBOX.JS < prev    next >
Text File  |  2008-01-24  |  6KB  |  249 lines

  1.  
  2. var _CHECKBOX_CHECKED = "0";
  3. var _CHECKBOX_UNCHECKED = "1";
  4. var _CHECKBOX_DISABLE_CHECKED = "2";
  5. var _CHECKBOX_DISABLE_UNCHECKED = "3";
  6. var _CHECKBOX_MIX = "4";
  7.  
  8. var __HIDDEN = "<input type=\"hidden\"  id=\"{id}\" value=\"{value}\" />";
  9. var __IMAGE = "<img src=\"{image}\" id=\"{id}\"  onmouseover=\"{mouseover}\" onmouseout=\"{mouseout}\"  onclick=\"{onclick}{EVENT}\" align=\"absmiddle\"  style=\"cursor:hand;\" />"; 
  10.  
  11.  
  12. function CheckBox()
  13. {
  14.   this.ID = "";
  15.   
  16.   // if the id is passed as an argument 
  17.   if( arguments.length == 1 )
  18.     this.ID = this.ID = arguments[0];
  19.     
  20.   this.State =   _CHECKBOX_UNCHECKED;
  21.   this.lblText = new Label();
  22.   this.lblText.Text = "";
  23.   
  24.   this.OnClickEvent = "__OnClickCheckBox"; 
  25.   this.OnMouseOver = "__OnMouseOverCheckBox";
  26.   this.OnMouseOut = "__OnMouseOutCheckBox";
  27.   
  28.   this.OnClickCustomEvent = "";
  29.   
  30.   this.Render = __RenderCheckBox;
  31.   this.SetState = __SetCheckBoxState;
  32.   this.GetState = __GetCheckBoxState;
  33.   this.Enable = __EnableCheckBox;
  34.   this.Check = __CheckCheckBox;
  35.   
  36.   this.IsChecked = __IsCheckedCheckBox;
  37.   this.Toggle = __ToggleCheckBox;
  38.   this.IsRendered  = __CheckBoxIsRendered;
  39. }
  40.  
  41.  function __CheckBoxIsRendered()
  42. {
  43.   return  Get( "hid_"+ this.ID ) == null ? false : true;
  44. }
  45.  
  46. function __RenderCheckBox()
  47. {
  48.   // replace hidden field's id and value
  49.   var hid = __HIDDEN.replace("{id}","hid_"+ this.ID);
  50.   hid = hid.replace("{value}",this.State);
  51.   
  52.   // replace image's id, src and onclick event
  53.   var image = __IMAGE.replace("{id}","image_"+ this.ID);
  54.   image = image.replace("{image}",__GetCheckBoxImage(this.State ) );
  55.   image = image.replace("{onclick}", this.OnClickEvent + "('hid_"+ this.ID +"','image_"+this.ID+"');" );
  56.   image = image.replace("{mouseover}", this.OnMouseOver + "('hid_"+ this.ID +"','image_"+this.ID+"');" );
  57.   image = image.replace("{mouseout}", this.OnMouseOut + "('hid_"+ this.ID +"','image_"+this.ID+"');" );
  58.   
  59.   image = image.replace("{EVENT}",this.OnClickCustomEvent );
  60.  
  61.   
  62.   
  63.   this.lblText.ID = "chktext_" + this.ID;
  64.   this.lblText.OnClickEvent = this.OnClickEvent;  
  65.  
  66.   var text = ((this.lblText.Text == "" )? "": this.lblText.Render());
  67.   
  68.   return image + " " + text + hid;
  69. }
  70.  
  71. function __SetCheckBoxState( state )
  72.   var img = Get( "image_" + this.ID );
  73.   img.src = __GetCheckBoxImage ( state ); // change image
  74.   
  75.    Get( "hid_"+ this.ID ).value = state; // change hidden value
  76. }
  77.  
  78. function __GetCheckBoxState()
  79. {
  80.   return Get( "hid_"+ this.ID ).value;
  81. }
  82.  
  83. // value = boolean
  84. function __EnableCheckBox( value )
  85. {
  86.  var state = Get( "hid_"+ this.ID ).value; // get current state 
  87.  var temstate = state;
  88.  
  89.  if( value ) // if enable = true
  90.  { 
  91.    switch( state )
  92.    {
  93.      case _CHECKBOX_DISABLE_CHECKED:
  94.       temstate = _CHECKBOX_CHECKED;
  95.       break;
  96.       
  97.     case _CHECKBOX_DISABLE_UNCHECKED:
  98.       temstate = _CHECKBOX_UNCHECKED;
  99.       break;
  100.    }
  101.  }else
  102.  {
  103.   switch( state ) // if enable = false
  104.    {
  105.      case _CHECKBOX_CHECKED:
  106.       temstate = _CHECKBOX_DISABLE_CHECKED;
  107.       break;
  108.       
  109.     case _CHECKBOX_UNCHECKED:
  110.       temstate = _CHECKBOX_DISABLE_UNCHECKED;
  111.       break;
  112.    }
  113.  }
  114.  Get( "hid_"+ this.ID ).value = temstate; // change hiddeb value (state )
  115.  Get( "image_"+ this.ID ).src = __GetCheckBoxImage(temstate ); // change image
  116.  
  117. }
  118.  
  119.  
  120.  
  121. // value = boolean
  122. function __CheckCheckBox( value )
  123. {
  124.  var state = Get( "hid_"+ this.ID ).value; // get current state 
  125.  var temstate = (value == true )?  _CHECKBOX_CHECKED : _CHECKBOX_UNCHECKED ;
  126.  
  127.  
  128.  if( (state == _CHECKBOX_CHECKED) || (state == _CHECKBOX_UNCHECKED) )
  129.  {
  130.   Get( "hid_"+ this.ID ).value = temstate; // change hiddeb value (state )
  131.   Get( "image_"+ this.ID ).src = __GetCheckBoxImage(temstate ); // change image
  132.  }
  133. }
  134.  
  135.  
  136. function __IsCheckedCheckBox()
  137. {
  138.    if( Get( "hid_"+ this.ID  ) == null )
  139.      return false; 
  140.    return __GetCheckBoxValue( Get( "hid_"+ this.ID ).value);
  141. }
  142.  
  143. // toggle the checkbox state 
  144. function __ToggleCheckBox()
  145. {
  146.    __OnClickCheckBox("hid_"+ this.ID, "image_"+ this.ID );
  147. }
  148.  
  149. //************************************ helper functions ************************************************************
  150.  
  151. function __OnClickCheckBox(hidid, imageid)
  152. {
  153.  var hid = Get( hidid ).value; // get current state
  154.  
  155.  if ( hid ==  _CHECKBOX_UNCHECKED )
  156.    {
  157.      Get( hidid ).value = _CHECKBOX_CHECKED;
  158.      Get(imageid ).src = __GetCheckBoxImage( _CHECKBOX_CHECKED );
  159.    }
  160.    
  161.   if ( hid ==  _CHECKBOX_CHECKED )
  162.    {
  163.      Get( hidid ).value = _CHECKBOX_UNCHECKED;
  164.      Get(imageid ).src = __GetCheckBoxImage( _CHECKBOX_UNCHECKED );
  165.    } 
  166. }
  167.  
  168. function __OnMouseOverCheckBox(hidid, imageid)
  169. {
  170.   var hid = Get( hidid ).value; // get current state
  171.   Get(imageid ).src = __GetCheckBoxImage( hid, true );
  172. }
  173.  
  174.  
  175. function __OnMouseOutCheckBox(hidid, imageid)
  176. {
  177.   var hid = Get( hidid ).value; // get current state
  178.   Get(imageid ).src = __GetCheckBoxImage( hid );
  179. }
  180.  
  181. //return checkbox boolean value, for given state
  182. function __GetCheckBoxValue( state )
  183. {
  184.   switch( state )
  185.   {
  186.     case _CHECKBOX_CHECKED:
  187.     case _CHECKBOX_DISABLE_CHECKED:
  188.      return true;
  189.      break;
  190.     
  191.     case _CHECKBOX_UNCHECKED:
  192.     case _CHECKBOX_DISABLE_UNCHECKED:
  193.      return false;
  194.      break;
  195.   }
  196. }
  197.  
  198. //return image for the given state
  199. //mouseover : bool
  200. function __GetCheckBoxImage( state, mouseover )
  201. {
  202.   var m = mouseover || false;
  203.   switch( state )
  204.   {
  205.     case _CHECKBOX_CHECKED:
  206.      return m ?  "P_check_selectedhover.gif" : "P_check_selected.gif";
  207.      break;
  208.     case _CHECKBOX_DISABLE_CHECKED:
  209.      return "checkbox_checked_disable.JPG";
  210.      break;
  211.     case _CHECKBOX_MIX:
  212.      return m ? "P_check_mixedhover.gif" : "P_check_mixed.gif";
  213.      break;
  214.     case _CHECKBOX_DISABLE_UNCHECKED:
  215.      return "checkbox_unchecked_disable.JPG";
  216.      break;
  217.     case _CHECKBOX_UNCHECKED:
  218.      return m ? "P_check_unselectedhover.gif" : "P_check_unselected.gif";
  219.      break;
  220.   }
  221. }
  222.  
  223.  
  224. //*******************************************************************
  225.  
  226.  
  227. function c1()
  228. {
  229.  var c = new CheckBox("XXX");
  230.  
  231.  Get("div").innerHTML = c.Render();
  232. }
  233. function c2()
  234. {
  235.  var c = new CheckBox("XXX");
  236.  c.SetState( _CHECKBOX_MIX );
  237. }
  238.  
  239. function c3()
  240. {
  241.   var c = new CheckBox("XXX");
  242.  c.Enable(true);
  243. }
  244. function c4()
  245. {
  246. var c = new CheckBox("XXX");
  247.  c.Enable(false);
  248. }